package com.quanxiaoha.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.quanxiaoha.weblog.common.domain.dos.UserDO;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * @author: 犬小哈
 * @url: www.quanxiaoha.com
 * @date: 2023-08-22 17:06
 * @description: 用户 Mapper
 **/
public interface UserMapper extends BaseMapper<UserDO> {
    
    /**
     * 根据用户名查找用户
     */
    default UserDO findByUsername(String username) {
        LambdaQueryWrapper<UserDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserDO::getUsername, username);
        return selectOne(wrapper);
    }

    /**
     * 根据邮箱查找用户
     */
    default UserDO findByEmail(String email) {
        if (!StringUtils.hasText(email)) {
            return null;
        }
        LambdaQueryWrapper<UserDO> wrapper = Wrappers.<UserDO>lambdaQuery()
                .eq(UserDO::getEmail, email)
                .eq(UserDO::getIsDeleted, false);
        return selectOne(wrapper);
    }

    /**
     * 根据用户名更新密码
     */
    default int updatePasswordByUsername(String username, String password) {
         LambdaUpdateWrapper<UserDO> wrapper = new LambdaUpdateWrapper<>();
         // 设置要更新的字段
         wrapper.set(UserDO::getPassword, password);
         wrapper.set(UserDO::getUpdateTime, LocalDateTime.now());
         // 更新条件
         wrapper.eq(UserDO::getUsername, username);

         return update(null, wrapper);
    }

    /**
     * 更新用户登录信息
     */
    default int updateLoginInfo(Long userId, LocalDateTime loginTime) {
        LambdaUpdateWrapper<UserDO> wrapper = Wrappers.<UserDO>lambdaUpdate()
                .set(UserDO::getLastLoginTime, loginTime)
                .setSql("login_count = login_count + 1")
                .set(UserDO::getUpdateTime, LocalDateTime.now())
                .eq(UserDO::getId, userId);
        
        return update(null, wrapper);
    }

    /**
     * 更新用户个人信息
     */
    default int updateUserProfile(Long userId, String nickname, String avatar, String bio, String email, String phone) {
        LambdaUpdateWrapper<UserDO> wrapper = Wrappers.<UserDO>lambdaUpdate()
                .set(UserDO::getUpdateTime, LocalDateTime.now())
                .eq(UserDO::getId, userId);
        
        if (StringUtils.hasText(nickname)) {
            wrapper.set(UserDO::getNickname, nickname);
        }
        if (StringUtils.hasText(avatar)) {
            wrapper.set(UserDO::getAvatar, avatar);
        }
        if (StringUtils.hasText(bio)) {
            wrapper.set(UserDO::getBio, bio);
        }
        if (StringUtils.hasText(email)) {
            wrapper.set(UserDO::getEmail, email);
        }
        if (StringUtils.hasText(phone)) {
            wrapper.set(UserDO::getPhone, phone);
        }
        
        return update(null, wrapper);
    }

    /**
     * 验证邮箱
     */
    default int verifyEmail(Long userId) {
        LambdaUpdateWrapper<UserDO> wrapper = Wrappers.<UserDO>lambdaUpdate()
                .set(UserDO::getEmailVerified, true)
                .set(UserDO::getUpdateTime, LocalDateTime.now())
                .eq(UserDO::getId, userId);
        
        return update(null, wrapper);
    }
}
